package com.client.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.client.mapper.ClientMapper;
import com.client.service.IClientService;
import com.common.entity.Client;
import com.common.untils.CommonResult;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pgx
 * @since 2022-09-05
 */
@Service
public class ClientServiceImpl implements IClientService {
    @Autowired
    private ClientMapper clientMapper;
    @Override
    public CommonResult insertClinet(String mobile, String qianPassword) {
        QueryWrapper<Client> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);

        Client client = clientMapper.selectOne(queryWrapper);
        if (client!=null){
            return new CommonResult(5000,"该手机号已被注册",null);
        }
        String replace = UUID.randomUUID().toString().replace("-", "");
        Client client1 = new Client();
        client1.setUsername(replace);
        String s = UUID.randomUUID().toString();
        client1.setSalt(s);
        SimpleHash simpleHash = new SimpleHash("MD5",qianPassword,client1.getSalt(),1024);
        String s1 = simpleHash.toString();
        client1.setPassword(s1);
        client1.setBid(0);
        client1.setMobile(mobile);
        int insert = clientMapper.insert(client1);
        if (insert==1){
            return  new CommonResult(2000,"注册成功",null);
        }
        return new CommonResult(5000,"注册失败",null);
    }

    @Override
    public CommonResult findStudentBymobile(String mobile) {
        QueryWrapper<Client> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Client client = clientMapper.selectOne(wrapper);
        return new CommonResult(2000,"查找成功",client);
    }

    @Override
    public CommonResult modifyStudentInformation(Client client) {
        QueryWrapper<Client> clientQueryWrapper = new QueryWrapper<>();
        clientQueryWrapper.eq("mobile",client.getMobile());
        clientMapper.update(client,clientQueryWrapper);
        return new CommonResult(2000,"修改成功",null);
    }

    @Override
    public CommonResult updateMemberPassword(String mobile, String newPassword) {
        QueryWrapper<Client> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        Client client = clientMapper.selectOne(queryWrapper);
        SimpleHash simpleHash = new SimpleHash("MD5",newPassword,client.getSalt(),1024);
        String s1 = simpleHash.toString();
        client.setPassword(s1);
        int update = clientMapper.update(client, queryWrapper);
        if (update==1){
            return new CommonResult(2000,"修改密码成功",null);
        }
        return new CommonResult(5000,"修改密码失败",null);
    }
}
